package com.eshipenet.shipnet.shiprpc.ignite;

import com.eshipenet.shipnet.ship.Ship;
import com.eshipenet.shipnet.ship.ShipGovernor;
import com.eshipenet.shipnet.ship.ShipGovernorLink;
import com.eshipenet.shipnet.ship.ShipOwner;
import com.eshipenet.shipnet.shiprpc.service.*;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.CacheConfiguration;

public class ShipIgnite {

    public  static  Ignite ignite;

    public synchronized static Ignite ignite() {

        if (ignite == null){
            ignite = Ignition.start("Ship-server.xml");

            CacheConfiguration<String, ShipGovernor> cacheCfg = new CacheConfiguration<String, ShipGovernor>("ShipGovernor");
            cacheCfg.setIndexedTypes(String.class, ShipGovernor.class);
            IgniteCache<String, ShipGovernor> shipGovernorCache = ignite.getOrCreateCache(cacheCfg);
            ignite.services().deployMultiple("ShipGovernorService", new ShipGovernorServiceImp(shipGovernorCache), 1, 1);

            CacheConfiguration<String, ShipOwner> shipOwnerCacheCfg = new CacheConfiguration<String, ShipOwner>("ShipOwner");
            shipOwnerCacheCfg.setIndexedTypes(String.class, ShipOwner.class);
            IgniteCache<String, ShipOwner> shipOwnerCache = ignite.getOrCreateCache(shipOwnerCacheCfg);
            ignite.services().deployMultiple("ShipOwnerService", new ShipOwnerServiceImp(shipOwnerCache), 1, 1);

            CacheConfiguration<String, Ship> shipCacheCfg = new CacheConfiguration<String, Ship>("Ship");
            shipCacheCfg.setIndexedTypes(String.class, Ship.class);
            IgniteCache<String, Ship> shipCache = ignite.getOrCreateCache(shipCacheCfg);
            ignite.services().deployMultiple("ShipService", new ShipServiceImp(shipCache), 1, 1);

            CacheConfiguration<String, ShipGovernorLink> shipGovernorLinkCacheCfg = new CacheConfiguration<String, ShipGovernorLink>("ShipGovernorLink");
            shipGovernorLinkCacheCfg.setIndexedTypes(String.class, ShipGovernorLink.class);
            IgniteCache<String, ShipGovernorLink> shipGovernorLinkCache = ignite.getOrCreateCache(shipGovernorLinkCacheCfg);
            ignite.services().deployMultiple("ShipGovernorLinkService", new ShipGovernorLinkServiceImp(shipGovernorLinkCache), 1, 1);
        }

        return ignite;
    }


}
